//
//  ViewController.m
//  AVFloatView
//
//  Created by hulinhu on 2020/10/21.
//

#import "ViewController.h"
#import <AVKit/AVKit.h>
#import "SubViewController.h"
#import "PiPViewManager.h"

@interface ViewController ()<AVPictureInPictureControllerDelegate>

@property (nonatomic, strong) AVPlayerLayer *playerLayer;
@property (nonatomic, strong) AVPictureInPictureController *pipController;
@property (nonatomic, strong) UIButton *changeButton;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 切换按钮
    UIButton *changeButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
    changeButton2.backgroundColor = [UIColor blueColor];
    changeButton2.layer.cornerRadius = 8;
    changeButton2.layer.masksToBounds = YES;
    changeButton2 .frame = CGRectMake(40, 200, self.view.frame.size.width-80, 40);
    [changeButton2 setTitle:@"视频播放页" forState:UIControlStateNormal];
    [changeButton2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [changeButton2 addTarget:self action:@selector(closePipBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeButton2];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // 延时销毁 系统弹窗
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[PiPViewManager sharedSingleton].pipPlayController stopPlay];
        [PiPViewManager sharedSingleton].pipPlayController = nil;
    });
}


-(void)closePipBtn:(UIButton *)sender {
    SubViewController *subVC = [[SubViewController alloc] init];
    [self.navigationController pushViewController:subVC animated:YES];
}


@end
